package org.example.web;

import com.alibaba.fastjson.JSON;

import org.example.pojo.Ipfs;

import org.example.service.IpfsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(name = "Add", value = "/Add")
public class Add extends HttpServlet {
    IpfsService ipfsService = new IpfsService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Add");
        //1.接收数据   request.getParameter不能接收JSON数据
        //String brandName = request.getParameter("brandName");
        request.setCharacterEncoding("utf-8");
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);
        Ipfs ipfs = JSON.parseObject(s, Ipfs.class);
        System.out.println(ipfs);

        //
        ipfsService.add(ipfs);
        response.getWriter().write("success");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);

    }
}
